package com.cimp.service.tools;

import com.cimp.domain.tools.FtpConfig;
import com.cimp.domain.tools.UpgradeConfig;
import com.cimp.domain.tools.UpgradeParam;
import com.cimp.domain.tools.UpgradeResult;
import com.cimp.service.tools.async.AutoUpgradeRunnable;
import com.cimp.util.common.svn.SVNManager;
import com.cimp.websocket.UpgradeHandlerProcessor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * [简要描述]: 工具包服务层
 * [详细描述]:
 *
 * @author huojinhao
 * @date 2018/11/14
 */
@Service
public class ToolsService {

    /**
     * SVN 服务管理器
     */
    @Resource
    private SVNManager svnManager;

    /**
     * 一键升级线程句柄
     */
    private Thread autoUpgradeThread;

    /**
     * 一键升级交互参数
     */
    private UpgradeParam upgradeParam;

    /**
     * 一键升级配置参数
     */
    @Resource
    private UpgradeConfig upgradeConfig;


    /**
     * 一键升级
     */
    public void upgrade(UpgradeParam param) throws Exception {
        if (autoUpgradeThread != null && autoUpgradeThread.isAlive()) {
            UpgradeHandlerProcessor.sendMessage(UpgradeResult.ing());
            return;
        }
        this.upgradeParam = param;
        autoUpgradeThread = new Thread(new AutoUpgradeRunnable(svnManager, upgradeParam, upgradeConfig));
        autoUpgradeThread.start();
    }

    public UpgradeParam getUpgradeParam(){
        if (this.upgradeParam == null) {
            this.upgradeParam = new UpgradeParam();
        }
        upgradeParam.setSvnPre(svnManager.getSvnConfig().getUrl());

        return this.upgradeParam;
    }

}
